#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "status.h"


static cJSON* root;

PosStatus posStatus;

/**
从文件中读取json信息到结构体
*/
int LoadStatus(PosStatus *status){
	printf("LoadStatus ENTER\n");
	root = GetJsonObject(STATUS_FILE_NAME);
	char * out = cJSON_Print(root);
	printf("LoadStatus:json=%s\n",out);
    if(out == NULL){
        printf("LoadStatus error,no json data\n");
        return -1;
    }
    char * item = NULL;
    item = GetValueString(root,"sn");
    if(item != NULL){
        strcpy(status->sn,item);
        printf("[item] = %s\n",status->sn);
    }
    item = GetValueString(root,"posno");
    if(item != NULL){
        strcpy(status->posno,item);
        printf("[item] = %s\n",status->posno);
    }
    item = GetValueString(root,"city");
    if(item != NULL){
        strcpy(status->city,item);
        printf("[item] = %s\n",status->city);
    }
    item = GetValueString(root,"tyid");
    if(item != NULL){
        strcpy(status->tyid,item);
        printf("[item] = %s\n",status->tyid);
    }
    item = GetValueString(root,"sver");
    if(item != NULL){
        strcpy(status->sver,item);
        printf("[item] = %s\n",status->sver);
    }

	item = GetValueString(root,"ctime");
	if(item != NULL){
		strcpy(status->ctime,item);
		printf("[item] = %s\n",status->ctime);
	}

   
	return 0;
}

/**
结构体信息写入json文件
*/
int SaveStatus(PosStatus status){
	char * out = NULL;
    printf("SaveStatus ENTER\n");
	if(root == NULL){
		printf("SaveStatus error\n");
		return -1;
	}
    SetValueString(root,"sn",status.sn);
    SetValueString(root,"posno",status.posno);
    SetValueString(root,"tyid",status.tyid);
	SetValueString(root,"city",status.city);

    SetValueString(root,"sver",status.sver);


	SetValueInt(root,"unum1",status.unum1);
    SetValueInt(root,"unum2",status.unum2);
    SetValueInt(root,"amount",status.amount);
    SetValueInt(root,"count",status.count);
    SetValueInt(root,"line",status.line);

	SetValueString(root,"carno",status.carno);
    SetValueString(root,"jd",status.jd);
    SetValueString(root,"wd",status.wd);

    SetValueInt(root,"alarm",status.alarm);
	SetValueInt(root,"pver",status.pver);
	SetValueInt(root,"bver",status.bver);

	SetValueString(root,"empid",status.empid);
	SetValueString(root,"rvs1",status.rvs1);
	SetValueString(root,"rvs2",status.rvs2);

    SetValueString(root,"stime",status.stime);
    SetValueString(root,"ctime",status.ctime);

    SetValueInt(root,"tenant",status.tenant);

	printf("SaveStatus end\n");

	out = cJSON_Print(root);
	writeFile(STATUS_FILE_NAME,out);
	printf("SetStatus:json=%s\n",out);
	return 0;
}

int writeFile(char *filename,char *data){
	FILE *fp = NULL;
 
	fp = fopen(filename,"w+");
	if(fp == NULL){
		fprintf(stderr,"open file failed\n");
		return -1;
	}
	fprintf(fp,"%s",data);
 
	if(fp != NULL){
		fclose(fp);
	}
	return 0;
}

cJSON* GetJsonObject(char* fileName){
    long len;
    char* pContent;
    int tmp;
    cJSON* json;

    FILE* fp = fopen(fileName, "rb+");
    if(!fp){
        printf("open file error\n");
        return NULL;
    }
    printf("open file success\n");
    fseek(fp,0,SEEK_END);
    len=ftell(fp);
    if(0 == len){
        printf("file len is 0\n");
        return NULL;
    }
   
    fseek(fp,0,SEEK_SET);
    pContent = (char*) malloc (sizeof(char)*len);
    tmp = fread(pContent,1,len,fp);

    fclose(fp);
    json = cJSON_Parse(pContent);
    if (!json)
    {
        free(pContent);
        return NULL;
    }
    free(pContent);
    printf("read file ok\n");
    return json;
}

//读取cJSON索引为index的结点某个key值对应的value，索引从0开始

int GetArrayValueString(cJSON* json,int id, char* name, char* param){
    cJSON* node;
    node = cJSON_GetArrayItem(json,id);
    if(!node){
        return -1;
    }
    sprintf(param, "%s", cJSON_GetObjectItem(node, name)->valuestring);
    return 0 ;
}

char* GetValueString(cJSON* json,char* name){
	cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
    	return pObj->valuestring;
    }else{
    	return NULL;
    }
}

int GetValueInt(cJSON* json,char* name){
	cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
    	return pObj->valueint;
    }else{
    	return 0;
    }
}

int SetValueString(cJSON* json,char* name,char* value){
	strcpy(cJSON_GetObjectItem(json,name)->valuestring,value);
	return 0;
}

int SetValueInt(cJSON* json,char* name,int value){
	cJSON_GetObjectItem(json,name)->valueint = value;
	cJSON_GetObjectItem(json,name)->valuedouble = value;
	return 0;
}


/*
int main(){
	printf("cjson test,read and save\n");
	LoadStatus(&posStatus);

    strcpy(posStatus.sver,"NC_B503_YYZ");
    posStatus.alarm = 6;
    posStatus.amount = 10;
    posStatus.count = 12;
    strcpy(posStatus.posno,"413101010E");

	SaveStatus(posStatus);
}
*/
